package cn.hx.mall.sell.controller;

import cn.hx.mall.sell.entity.SmsHomeNewProduct;
import cn.hx.mall.sell.service.SmsHomeNewProductService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 新鲜好物表(SmsHomeNewProduct)表控制层
 *
 * @author makejava
 * @since 2022-11-02 16:10:32
 */
@Controller
@RequestMapping("sell")
public class SmsHomeNewProductController {
    /**
     * 服务对象
     */
    @Resource
    private SmsHomeNewProductService smsHomeNewProductService;

    @GetMapping("/newProduct")
    public String newProList(ModelMap map, SmsHomeNewProduct smsHomeNewProduct,
                             @RequestParam(value = "page", defaultValue = "1") Integer page,
                             @RequestParam(value = "size", defaultValue = "5") Integer size) {
        Page<SmsHomeNewProduct> smsHomeNewProducts = PageHelper.startPage(page, size);
        smsHomeNewProductService.queryAll(smsHomeNewProduct);
        PageInfo<SmsHomeNewProduct> pageInfo = new PageInfo<>(smsHomeNewProducts);
        map.addAttribute("newProducts", pageInfo);
        return "sell/newProduct";
    }

    @PostMapping("selectNewProduct")
    public String newList(ModelMap map, SmsHomeNewProduct smsHomeNewProduct,
                          @RequestParam(value = "page", defaultValue = "1") Integer page,
                          @RequestParam(value = "size", defaultValue = "5") Integer size) {
        Page<SmsHomeNewProduct> smsHomeNewProducts = PageHelper.startPage(page, size);
        smsHomeNewProductService.queryAll(smsHomeNewProduct);
        PageInfo<SmsHomeNewProduct> pageInfo = new PageInfo<>(smsHomeNewProducts);
        map.addAttribute("newProducts", pageInfo);
        return "sell/newProduct";
    }

    @GetMapping("/addNewProduct")
    public String add() {
        return "sell/addNewProduct";
    }

    @PostMapping("/addNewProduct")
    public String brand(ModelMap map, SmsHomeNewProduct smsHomeNewProduct) {
        smsHomeNewProductService.insert(smsHomeNewProduct);
        map.addAttribute("newProduct", smsHomeNewProductService.queryAllByLimit(0, 1000));
        return "redirect:/sell/newProduct";
    }

    @PostMapping("/updateNewProductSort")
    public String save(SmsHomeNewProduct smsHomeNewProduct,String path) {
        smsHomeNewProductService.update(smsHomeNewProduct);
        return "redirect:/"+path;
    }

    @GetMapping("/selectByNewProductId/{id}")
    @ResponseBody
    public Map<String, Object> queryById(@PathVariable Integer id) {
        SmsHomeNewProduct smsHomeNewProduct = smsHomeNewProductService.queryById(id);
        Map<String, Object> resMap = new HashMap<>();
        resMap.put("data", smsHomeNewProduct);
        return resMap;
    }

    /*状态按钮改变*/
    @GetMapping("statusByNewId/{id}")
    @ResponseBody
    public Map<String, Object> statusById(@PathVariable Integer id) {
        SmsHomeNewProduct smsHomeNewProduct = smsHomeNewProductService.queryById(id);
        Integer status = smsHomeNewProduct.getRecommendStatus();
        if (status.equals(1)) {
            smsHomeNewProduct.setRecommendStatus(0);
        } else {
            smsHomeNewProduct.setRecommendStatus(1);
        }
        HashMap<String, Object> map = new HashMap<>();
        SmsHomeNewProduct update = smsHomeNewProductService.update(smsHomeNewProduct);
        if (update != null) {
            map.put("code", 1);
        }
        return map;
    }

    @GetMapping("deleteByNewId/{id}")
    @ResponseBody
    public Map<String, Object> delete(@PathVariable Integer id) {
        boolean b = smsHomeNewProductService.deleteById(id);
        HashMap<String, Object> map = new HashMap<>();
        if (b) {
            map.put("code", 1);
        }
        return map;
    }
}